<?php

namespace App\Http\Controllers\Admin;

use App\Services\ArticleService;
use App\Services\CategoryService;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller;

class ArticleController extends Controller
{

    private $articleService;
    private $categoryService;

    public function __construct(ArticleService $articleService, CategoryService $categoryService)
    {
        $this->articleService = $articleService;
        $this->categoryService = $categoryService;
    }

    public function index()
    {
        $cate = $this->categoryService->getList('id,name');
        return view('admin.article.index', ['cate' => $cate]);
    }

    public function list(Request $request)
    {
        $this->validate($request, [
            'id'          => 'bail|integer',
            'title'       => 'bail|string',
            'category_id' => 'bail|integer',
            'status'      => 'bail|integer',
            'limit'       => 'bail|integer',
        ]);

        $where = [];

        if ($request->filled('id')) {
            $where[] = ['id', $request->input('id')];
        }

        if ($request->filled('title')) {
            $title = $request->input('title');
            $title = str_replace(['%', '_'], ['\%', '\_'], $title);
            $where[] = ['title', 'like', '%' . $title . '%'];
        }

        if ($request->filled('category_id')) {
            $where[] = ['category_id', $request->input('category_id')];
        }

        if ($request->filled('status')) {
            $where[] = ['status', $request->input('status')];
        }

        $limit = $request->input('limit', 15);
        $columns = 'id, title, category_id, created_at, updated_at, status, published_at';
        $data = $this->articleService->getList($where, $limit, $columns);

        return success($data);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('get')) {
            $cate = $this->categoryService->getList('id,name');
            return view('admin.article.add', ['cate' => $cate]);
        }

        $this->validate($request, [
            'title'        => 'bail|required|string|max:100',
            'category_id'  => 'bail|required|integer',
            'tags'         => 'bail|string|max:150',
            'content'      => 'bail|required|string|max:16777215',
            'excerpt'      => 'bail|string|max:65535',
            'show_comment' => 'bail|integer|in:0,1',
            'is_original'  => 'bail|integer|in:0,1',
            'status'       => 'bail|required|integer|in:0,1,2',
            'related_ids'  => 'bail|string|regex:/^\d+(,\d+)*$/',
        ]);

        $data = $request->input();
        $this->articleService->add($data);

        return success();
    }

    public function edit(Request $request)
    {
        if ($request->isMethod('get')) {
            $id = $request->input('id', 0);
            $article = $this->articleService->find($id);
            $data = $article->toArray();
            $data['relatedArticles'] = $article->getRelatedArticles();
            $data['cate'] = $this->categoryService->getList('id,name');
            return view('admin.article.edit', $data);
        }

        $this->validate($request, [
            'id'           => 'bail|required|integer',
            'title'        => 'bail|required|string|max:100',
            'category_id'  => 'bail|required|integer',
            'tags'         => 'bail|string|max:150',
            'content'      => 'bail|required|string|max:16777215',
            'excerpt'      => 'bail|string|max:65535',
            'show_comment' => 'bail|integer|in:0,1',
            'is_original'  => 'bail|integer|in:0,1',
            'status'       => 'bail|required|integer|in:0,1,2',
            'related_ids'  => 'bail|string|regex:/^\d+(,\d+)*$/',
        ]);

        $data = $request->input();
        $data['show_comment'] = $data['show_comment'] ?? 0;
        $data['is_original'] = $data['is_original'] ?? 0;
        $article = $this->articleService->edit($data);

        return success(['excerpt' => $article->excerpt]);
    }

    public function del(Request $request)
    {
        $this->validate($request, [
            'id'   => 'bail|required|integer',
            'type' => 'bail|required|integer|in:0,1',
        ]);

        $id = $request->input('id');
        $type = $request->input('type');

        $this->articleService->del($id, $type);

        return success();
    }

}
